package io.yuanqi.hgtcshop.controller;

import com.alibaba.fastjson.JSON;
import io.yuanqi.hgtcshop.entity.dos.Goods;
import io.yuanqi.hgtcshop.entity.dos.GoodsSku;
import io.yuanqi.hgtcshop.entity.enums.PageViewEnum;
import io.yuanqi.hgtcshop.entity.enums.ResultCode;
import io.yuanqi.hgtcshop.entity.param.GoodsSearchParams;
import io.yuanqi.hgtcshop.entity.vo.GoodReturnVO;
import io.yuanqi.hgtcshop.entity.vo.GoodsVO;
import io.yuanqi.hgtcshop.exception.ServiceException;
import io.yuanqi.hgtcshop.service.GoodsService;
import io.yuanqi.hgtcshop.entity.vo.ResultMessage;
import io.yuanqi.hgtcshop.service.GoodsSkuService;
import io.yuanqi.hgtcshop.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/buyer/goods/goods")
public class GoodsBuyerController  {


    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsSkuService goodsSkuService;


//    @ApiOperation(value = "获取商品分页列表")
    @GetMapping
    public ResultMessage<GoodReturnVO> getByPage(GoodsSearchParams goodsSearchParams) {
        log.info(".getByPage 请求参数为:{}", JSON.toJSONString(goodsSearchParams));
        GoodReturnVO goodsVOS = goodsService.queryByParams(goodsSearchParams);
//        log.info(".getByPage 返回的结果为：{}",JSON.toJSONString(goodsVOS));
        return ResultUtil.data(goodsVOS);
    }

    @GetMapping(value = "/get/{goodsId}")
    public ResultMessage<GoodsVO> get(@NotNull(message = "商品ID不能为空") @PathVariable("goodsId") String id) {
        return ResultUtil.data(goodsService.getGoodsVO(id));
    }

    @GetMapping(value = "/sku/{goodsId}/{skuId}") //todo 未完成
    public ResultMessage<Map<String, Object>> getSku(@NotNull(message = "商品ID不能为空") @PathVariable("goodsId") String goodsId,
                                                     @NotNull(message = "SKU ID不能为空") @PathVariable("skuId") String skuId) {
        try {
            // 读取选中的列表
            Map<String, Object> map = goodsSkuService.getGoodsSkuDetail(goodsId, skuId);
            return ResultUtil.data(map);
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.GOODS_ERROR.message(), e);
            return ResultUtil.error(ResultCode.GOODS_ERROR);
        }

    }

    @GetMapping(value = "/sku/{skuId}") //todo 未完成
    public ResultMessage<GoodsSku> getSkuBySkuId(@NotNull(message = "SKU ID不能为空") @PathVariable("skuId") String skuId) {
        try {
            // 读取选中的列表
            GoodsSku skuBySkuId = goodsSkuService.getSkuBySkuId(skuId);
            return ResultUtil.data(skuBySkuId);
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.GOODS_ERROR.message(), e);
            return ResultUtil.error(ResultCode.GOODS_ERROR);
        }
    }
}
